<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/6
 * Time: 13:40
 */

namespace app\applet\model;


use think\Db;
use think\Exception;
use WeixinPay\WeixinPay;

class Goods
{
    public function rebateClass($id)
    {
        $class = Db::name("store_apply")
            ->field("member_id")
            ->where(['sa_id'=>$id])
            ->find();
        $member_id = $class['member_id'];
        $data = Db::name("goods_class")
            ->field("gc_id as id,gc_name as name")
            ->where(['member_id'=>$member_id])
            ->select();
        return $data;
    }
    public function homePageGoodsStore($data,$current_page,$page_num)
    {
        #距离
        $longitude = $data['longitude'];
        $latitude = $data['latitude'];
        $store = Db::name("store_apply")
            ->field("sa_id,longitude,latitude")
            ->select();
        foreach ($store as $k=>$v)
        {
            $store_id = $v['sa_id'];
            $longitude1 = $v['longitude'];
            $latitude1 = $v['latitude'];
            $distance = round(get_distance($longitude,$latitude,$longitude1,$latitude1));
            Db::name("store_apply")
                ->where(['sa_id'=>$store_id])
                ->update(['distance'=>$distance]);
        }

        $field = "g.goods_id,g.store_id,g.member_id,g.g_name,g.limit_start_time,g.limit_end_time,g.cover_pic,g.g_type,g.sale_total,g.create_time,s.distance,s.company_name,i.copper_pop,i.silver_pop,i.gold_pop";
        $where['g.is_delete'] = 1;
        $where['g.status'] = 1;
        if(!empty($data['type']))
        {
            $where['g_type'] = $data['type'];
        }
        $return_data = Db::name("goods")
            ->alias(['mm_store_apply'=>'s',"mm_goods"=>"g","mm_goods_info"=>"i"])
            ->field($field)
            ->join('mm_store_apply',"g.store_id = s.sa_id")
            ->join('mm_goods_info',"i.goods_id = g.goods_id")
            ->where($where)
            ->order('s.distance asc,g.create_time desc')
            ->page($current_page,$page_num)
            ->select();
        foreach ($return_data as $k=>$v)
        {
            $spec = Db::name("goods_spec")
                ->field("cost_price,price,coin_price,gold_coin_num,proportion")
                ->where(['goods_id'=>$v['goods_id']])
                ->order(['price'=>"asc"])
                ->find();
            $return_integral = ($spec['price']-$spec['price']*($spec['proportion']/100))*0.5;
            $return_data[$k]['return_integral'] = sprintf("%01.1f", $return_integral);

            $return_data[$k]["cost_price"] = $spec['cost_price'];
            $return_data[$k]["price"] = $spec['price'];
            $return_data[$k]["coin_price"] = $spec['coin_price'];
            $return_data[$k]["gold_coin_num"] = $spec['gold_coin_num'];
            $return_data[$k]["proportion"] = $spec['proportion'];
        }
        $total_num = Db::name("goods")
            ->alias(['mm_store_apply'=>'s',"mm_goods"=>"g"])
            ->join('mm_store_apply',"g.store_id = s.sa_id")
            ->where($where)
            ->count();
        $list['current_page'] = $current_page;
        $list['total_page'] = ceil($total_num / $page_num);
        $list['page_num'] = $page_num;
        $list['total_num'] = $total_num;
        $list['data'] = $return_data;
        return $list;
    }
    public function homePageGoods($data,$current_page,$page_num)
    {
        #距离
        $longitude = $data['longitude'];
        $latitude = $data['latitude'];
        $store = Db::name("store_apply")
            ->field("sa_id,longitude,latitude")
            ->select();
        foreach ($store as $k=>$v)
        {
            $store_id = $v['sa_id'];
            $longitude1 = $v['longitude'];
            $latitude1 = $v['latitude'];
            $distance = round(get_distance($longitude,$latitude,$longitude1,$latitude1));
            Db::name("store_apply")
                ->where(['sa_id'=>$store_id])
                ->update(['distance'=>$distance]);
        }

        $field = "g.goods_id,g.store_id,g.member_id,g.g_name,g.limit_start_time,g.limit_end_time,g.cover_pic,g.g_type,g.sale_total,g.create_time,s.distance,s.company_name,i.copper_pop,i.silver_pop,i.gold_pop";
        $where['g.is_delete'] = 1;
        $where['g.status'] = 1;
        if(!empty($data['type']))
        {
            $where['g_type'] = $data['type'];
        }
        $return_data = Db::name("goods")
            ->alias(['mm_store_apply'=>'s',"mm_goods"=>"g","mm_goods_info"=>"i"])
            ->field($field)
            ->join('mm_store_apply',"g.store_id = s.sa_id")
            ->join('mm_goods_info',"i.goods_id = g.goods_id")
            ->where($where)
            ->order('s.distance asc,g.create_time desc')
            ->page($current_page,$page_num)
            ->select();
        foreach ($return_data as $k=>$v)
        {
            $spec = Db::name("goods_spec")
                ->field("cost_price,price,coin_price,gold_coin_num,proportion")
                ->where(['goods_id'=>$v['goods_id']])
                ->order(['price'=>"asc"])
                ->find();
            $return_integral = ($spec['price']-$spec['price']*($spec['proportion']/100))*0.5;
            $return_data[$k]['return_integral'] = sprintf("%01.1f", $return_integral);

            $return_data[$k]["cost_price"] = $spec['cost_price'];
            $return_data[$k]["price"] = $spec['price'];
            $return_data[$k]["coin_price"] = $spec['coin_price'];
            $return_data[$k]["gold_coin_num"] = $spec['gold_coin_num'];
            $return_data[$k]["proportion"] = $spec['proportion'];
        }
        $total_num = Db::name("goods")
            ->alias(['mm_store_apply'=>'s',"mm_goods"=>"g"])
            ->join('mm_store_apply',"g.store_id = s.sa_id")
            ->where($where)
            ->count();
        $list['current_page'] = $current_page;
        $list['total_page'] = ceil($total_num / $page_num);
        $list['page_num'] = $page_num;
        $list['total_num'] = $total_num;
        $list['data'] = $return_data;
        return $list;
    }
    #店招支付
    public function shopPayment($data)
    {

        #获取openid
        $member_data = Db::name("member")
            ->field("openid")
            ->where(['member_id'=>$data['member_id']])
            ->find();
        $openid = $member_data['openid']; #用户openid
        //获取用户资产
        $asset=Db::name("member_asset")->where(['member_id'=>$data['member_id']])->find();

        $pay_wx=$data['total_fee'];
        $pay_score=$data['score'];

        //金额判断
        if( $pay_score>0&& $pay_score>$asset['score']){
            returnJson(-2003,'积分可用余额不足');
        }
        unset($data['score']);
        $data['pay_wx']=$pay_wx;
        $data['pay_score']=$pay_score;
        ##订单编号
        $order_sn=date("Y").date("m").mt_rand(1,9).date("d").mt_rand(1,9).date("Hi").mt_rand(1000,9999);
        $body                = "店招商品时间付款";#订单描述
        $total_fee           = $pay_wx;#支付金额
        $data['order_sn']    = $order_sn;
        $data['body']        = $body;
        $data['create_time'] = time();
        $notify_url =  request()->domain()."/index.php/applet/Notify/shopPayment";
        $tm=time();
        Db::startTrans();
        try {
            #生成订单记录
            $orderID=Db::name("shop_sign_order")->insertGetId($data);
            if( $orderID<=0){
                throw new Exception("订单生成失败");
            }
            //日志
            if($pay_score>0){//写入积分冻结记录
                $num=Db::name("member_asset_log")
                    ->insert(['member_id'=>$data['member_id'],'type'=>3,'amount'=>$pay_score,
                        'source_type'=>2,'description'=>'商城待支付','change_type'=>1,'log_type_id'=>53,
                        'relation_id'=>$orderID,'create_time'=>$tm,'update_time'=>$tm]);
                if($num<=0){
                    throw new Exception("更新失败");
                }

                $sql="update mm_member_asset set score=score-$pay_score,frozen_score=frozen_score+$pay_score,pay_frozen_score=pay_frozen_score+$pay_score where member_id=".$data['member_id']." and score>=$pay_score ";
                $num=Db::query($sql);
                if($num<=0){
                    throw new Exception("更新失败");
                }
            }
            #调取支付接口
            $weixinPay = new WeixinPay($openid, $body, $total_fee, $order_sn,$notify_url);
            // 发起微信支付
            $result = $weixinPay->pay();
            $result['order_sn']=$order_sn;
            Db::commit();
            return $result;
        }catch (Exception $e){
            Db::rollback();
            returnJson(-2004,'订单生成失败');
        }
    }

    /**
     * 取消支付
     */
    public function cancelOrder($input){

        $order_sn=$input['order_sn'];
        $member_id=$input['member_id'];
        //获取当前记录
        $payInfo=Db::name("shop_sign_order")->where(['member_id'=>$member_id,"order_sn"=>$order_sn,'status'=>['not in',[1,2]]])->field("id,pay_score")->find();
        $score_money= $payInfo['pay_score'];

        $spID= $payInfo['id'];
        Db::startTrans();
        try {
            if($score_money>0){
                $sql="update mm_member_asset set score=score+$score_money,frozen_score=frozen_score-$score_money,pay_frozen_score=pay_frozen_score-$score_money where member_id=".$member_id." and frozen_score>=$score_money  and pay_frozen_score>=$score_money  ";
                $num=Db::query($sql);
                if($num<=0){
                    throw new Exception("更新失败");
                }
                $num=Db::name("member_asset_log")->where(['member_id'=>$member_id,'relation_id'=>$spID,"log_type_id"=>['in',[53,54]]])->delete();
                if($num<=0){
                    throw new Exception("更新失败");
                }
            }

            $num=Db::name("shop_sign_order")->where(['member_id'=>$member_id,"order_sn"=>$order_sn])->update(['status'=>2]);

            if($num<=0){
                throw new Exception("更新失败");
            }
            Db::commit();
            return ['order_sn'=>$order_sn];
        }catch (Exception $e){
            Db::rollback();
            returnJson(-2004,"操作失败");
        }
    }


    //设置店招
    public function recommendSet($data)
    {
        $type = $data['type'];
        $goods_id = $data['id'];
        #获取店铺id
        $store = Db::name("goods")
            ->field("store_id,g_type")
            ->where(['goods_id'=>$goods_id])
            ->find();
        $g_type = $store['g_type'];

        #店招每日费用
        $fee_day = Db::name("system_config")
            ->field("field_value")
            ->where(['field_name'=>"day_shop_sign"])
            ->find();
        #金币和积分
        $member_id = get_header_id("token");
        $asset = Db::name("member_asset")
            ->field("score,coin")
            ->where(['member_id'=>$member_id])
            ->find();
        $day_data['goods_id'] = $goods_id;
        $day_data['fee_day'] = $fee_day['field_value'];
        $day_data['score'] = $asset['score'];
        $day_data['coin'] = $asset['coin'];


        #设置店招数组
        $list['goods_id'] = $goods_id;
        $list['is_free'] = 1; #1表示免费店招
        $list['store_id'] = $store['store_id']; #1店铺id
        $list['g_type'] = $g_type; #商品类别
        $list['create_time'] = time();

        if($type == 1)
        {
            $count = Db::name("goods_shop_sign")
                ->where(["is_free"=>1,"g_type"=>$g_type,"store_id"=>$store['store_id']])
                ->count();
            if($count == 0)
            {
                #免费店招不存在的情况,判断该商品是否为收费店招,不是的话,直接设置为免费店招
                $free_goods = Db::name("goods_shop_sign")
                    ->field("is_delete,end_time")
                    ->where(['goods_id'=>$goods_id,"is_free"=>2])
                    ->find();
                if($free_goods)
                {
                    if($free_goods['is_delete'] == 1)
                    {
                        returnJson(-2004,"该商品已设置为收费店招");
                    }else{
                        if($free_goods['end_time'] > time())
                        {
                            Db::name("goods_shop_sign")
                                ->where(['goods_id'=>$goods_id])
                                ->update(['is_delete'=>1,'update_time'=>time()]);
                        }else{
                            returnJson(-2010,"店招过期,请去付款设置收费店招",$day_data);
                        }
                    }
                }else{
                    #直接设置为免费店招
                    Db::name("goods_shop_sign")
                        ->insert($list);
                }
            }else{
                $fee_goods = Db::name("goods_shop_sign")
                    ->where(['goods_id'=>$goods_id,"is_free"=>1])
                    ->count();
                if($fee_goods)
                {
                    returnJson(-2003,"该商品已设置免费店招");
                }
                returnJson(-2010,"请去付款设置收费店招",$day_data);
            }
            return "设置店招成功";
        }else{
            #取消店招
            $exit = Db::name("goods_shop_sign")
                ->field("is_free")
                ->where(['goods_id'=>$goods_id])
                ->find();

            if($exit['is_free'] == 1)
            {
                Db::name("goods_shop_sign")
                    ->where(['goods_id'=>$goods_id])
                    ->delete();
            }else{
                Db::name("goods_shop_sign")
                    ->where(['goods_id'=>$goods_id])
                    ->update(['is_delete'=>2,'update_time'=>time()]);
            }
            return "取消店招成功";
        }


    }
    #商铺商品分类
    public function shopGoodsClassDetail($id)
    {
        $data = Db::name("goods_class")
            ->field("gc_id as id,store_id,gc_name as name")
            ->where(['gc_id'=>$id])
            ->order(['create_time'=>"asc"])
            ->select();
        return $data;
    }
    public function shopGoodsClassDel($data)
    {
        $member_id = $data['member_id'];
        $gc_id = $data['id'];
        $count = Db::name("goods")
            ->where(['gc_id'=>$gc_id])
            ->count();
        if($count)
        {
            returnJson(-2004,"请先移除改分类下商品");
        }
        Db::name("goods_class")
            ->where(['member_id'=>$member_id,'gc_id'=>$gc_id])
            ->delete();
    }
    #商铺商品分类
    public function shopGoodsClassList($id)
    {
        $data = Db::name("goods_class")
            ->field("gc_id as id,gc_name as name")
            ->where(['member_id'=>$id])
            ->order(['create_time'=>"asc"])
            ->select();
        return $data;
    }
    #商铺商品分类保存
    public function shopGoodsClass($data)
    {
        #添加
        if(empty($data['id']))
        {
            $where['gc_name'] = $data['name'];
            $where['member_id'] = $data['member_id'];
            $count = self::onlyGoodsClass($where);
            if($count)
            {
                returnJson(-2004,"该商品分类已存在");
            }
            $class['gc_name'] = $data['name'];
            $class['member_id'] = $data['member_id'];
            $class['create_time'] = time();
            Db::name("goods_class")->insert($class);
        }else{
            #修改
            $name = $data['name'];;
            $member_id = $data['member_id'];;
            $id = $data['id'];
            $where = "gc_name = '$name' and member_id = '$member_id' and gc_id != '$id'";
            $where1 = "member_id = '$member_id' and gc_id = '$id'";
            $count = self::onlyGoodsClass($where);
            if($count)
            {
                returnJson(-2004,"该商品分类已存在");
            }
            $class['gc_name'] = $name;
            $class['update_time'] = time();
            Db::name("goods_class")
                ->where($where1)
                ->update($class);
        }
    }
    #商铺商品分类唯一性判断
    private function onlyGoodsClass($where)
    {
        $count = Db::name("goods_class")
            ->where($where)
            ->count();
        return $count;
    }
    //根据会员id消费爆品
    private function getHotGoods($id)
    {
        $data = Db::name("goods_shop_sign")
            ->alias(["mm_goods_shop_sign"=>"sign","mm_goods_info"=>"info","mm_goods"=>"g"])
            ->field("sign.goods_id,g.g_name,g.cover_pic,sale_total,g.store_id")
            ->join("mm_goods_info","info.goods_id =sign.goods_id")
            ->join("mm_goods","info.goods_id =g.goods_id")
            ->where(['sign.is_free'=>1,'g.member_id'=>$id,"g.g_type"=>1])
            ->find();
        if(!$data)
        {
            return [];
        }
        $spec = Db::name("goods_spec")
            ->field("price,cost_price,coin_price")
            ->where(['goods_id'=>$data['goods_id']])
            ->order(['price'=>"asc"])
            ->find();
        if($spec != null)
        {
            $data['price'] = $spec['price'];
            $data['cost_price'] = $spec['cost_price'];
            $data['coin_price'] = $spec['coin_price'];
        }
        return $data;


    }
    //商品分享上下级邀请处理
    public function goodsMember($goodsID,$invtID,$member_id){
        if($goodsID>0&&$invtID>0&&$member_id>0){
            $gType=Db::name('goods')->where(['goods_id'=>$goodsID])->value("g_type");
            if($gType==2){//消费爆品
                $num=Db::name("goods_member_inviter")->where(['goods_id'=>$goodsID,'member_id'=>$member_id,'inviter_id'=>$invtID])->count();
                if(empty($num)){
                    Db::name("goods_member_inviter")->insert(['goods_id'=>$goodsID,'member_id'=>$member_id,'inviter_id'=>$invtID]);
                }
            }

        }


     }

    //商品详情
    public function goodsDetail($id)
    {
        #商品类型
        $goods_type = self::getType($id);
        #爆款商城
        if($goods_type == 1)
        {
            $field = "
            s.goods_id,
            s.member_id,
            s.sale_total,
            in.video_path,
            in.img_list,
            s.g_name,
            s.limit_num_status,
            s.limit_number,
            s.limit_time_status,
            s.limit_start_time,
            s.limit_end_time,
            in.distribution_method,
            in.is_change,
            in.is_real,
            in.is_diff,
            in.product_details_image
            ";
            $field_spec = "gs_id,name,price,cost_price,coin_price,num";
            $data = Db::name("goods s")
                ->alias(['mm_goods_info'=>'in'])
                ->field($field)
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where(['s.goods_id'=>$id])
                ->find();
            #商品是否收藏
            $where1['member_id'] = get_header_id("token");
            $where1['object_id'] = $id;
            $where1['class_type'] = 15;
            $count = Db::name("collection")
                ->where($where1)
                ->count();
            if($count)
            {
                $is_collection = 1;
            }else{
                $is_collection = 0;
            }
            $data['is_collection'] = $is_collection;
            $data['video_path'] = json_decode($data['video_path'],true);
            $data['img_list'] = json_decode($data['img_list'],true);
            $data['product_details_image'] = json_decode($data['product_details_image'],true);
            $data['address_detail'] = self::getBaseStore($id)['address_detail'];
            $data['shop_name'] = self::getBaseStore($id)['company_name'];
            #规格
            $spec_data = Db::name("goods_spec")
                ->field($field_spec)
                ->where(['goods_id'=>$id])
                ->order(['price'=>"asc"])
                ->select();
            $data['spec'] = $spec_data;
            #联盟商家广告
            $advertisement_data = Db::name("goods_info")
                ->field("advertisement")
                ->where(['goods_id'=>$id])
                ->find();
            $advertisement_data =json_decode($advertisement_data['advertisement'],true)==null ? [] : json_decode($advertisement_data['advertisement'],true);
            if($advertisement_data)
            {
                foreach ($advertisement_data as $k=>$v)
                {
                    $data["advertisement"][] = self::getHotGoods($v['member_id']);
                }
                if(empty($data["advertisement"]))
                {
                    $data['advertisement'] = [];
                }

            }else{
                $data['advertisement'] = $advertisement_data;
            }



        }
        else if($goods_type == 2) {
            #裂变商品
            $field = "
            s.goods_id,
            s.sale_total,
            s.member_id,
            in.video_path,
            in.img_list,
            s.g_name,
            s.limit_num_status,
            s.limit_number,
            s.limit_time_status,
            s.limit_start_time,
            s.limit_end_time,
            in.distribution_method,            
            in.product_details_image,
            in.copper_money,
            in.copper_pop,
            in.silver_money,
            in.silver_pop,
            in.gold_money,
            in.gold_pop
            ";
            $field_spec = "gs_id,name,price,cost_price,num";
            $data = Db::name("goods s")
                ->alias(['mm_goods_info'=>'in'])
                ->field($field)
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where(['s.goods_id'=>$id])
                ->find();
            #商品是否收藏
            $where1['member_id'] = get_header_id("token");
            $where1['object_id'] = $id;
            $where1['class_type'] = 15;
            $count = Db::name("collection")
                ->where($where1)
                ->count();
            if($count)
            {
                $is_collection = 1;
            }else{
                $is_collection = 0;
            }
            $data['is_collection'] = $is_collection;
            $data['video_path'] = json_decode($data['video_path'],true);
            $data['img_list'] = json_decode($data['img_list'],true);
            $data['product_details_image'] = json_decode($data['product_details_image'],true);
            $data['address_detail'] = self::getBaseStore($id)['address_detail'];
            $data['shop_name'] = self::getBaseStore($id)['company_name'];
            #规格
            $spec_data = Db::name("goods_spec")
                ->field($field_spec)
                ->where(['goods_id'=>$id])
                ->order(['price'=>"asc"])
                ->select();
            $data['spec'] = $spec_data;
            #联盟商家广告
            $advertisement_data = Db::name("goods_info")
                ->field("advertisement")
                ->where(['goods_id'=>$id])
                ->find();
            $advertisement_data =json_decode($advertisement_data['advertisement'],true)==null ? [] : json_decode($advertisement_data['advertisement'],true);
            if($advertisement_data)
            {
                foreach ($advertisement_data as $k=>$v)
                {
                    if(self::getHotGoods($v['member_id']))
                    {
                        $data["advertisement"][] = self::getHotGoods($v['member_id']);
                    }
                    if(empty($data["advertisement"]))
                    {
                        $data['advertisement'] = [];
                    }
                }

            }else{
                $data['advertisement'] = $advertisement_data;
            }
        }
        else if($goods_type == 4) {
            #限时活动
            $field = "
            s.goods_id,
            s.sale_total,
            s.member_id,
            in.video_path,
            in.img_list,
            s.g_name,
            s.limit_number,
            s.limit_start_time,
            s.limit_end_time,
            in.distribution_method,
            in.is_change,
            in.is_real,
            in.is_diff,
            in.product_details_image,
            in.free_gift,
            in.consumption_pic,
            in.direct_push,
            in.inter_push
            ";
            $field_spec = "gs_id,price,cost_price";
            $data = Db::name("goods s")
                ->alias(['mm_goods_info'=>'in'])
                ->field($field)
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where(['s.goods_id'=>$id])
                ->find();
            #商品是否收藏
            $where1['member_id'] = get_header_id("token");
            $where1['object_id'] = $id;
            $where1['class_type'] = 15;
            $count = Db::name("collection")
                ->where($where1)
                ->count();
            if($count)
            {
                $is_collection = 1;
            }else{
                $is_collection = 0;
            }
            $data['is_collection'] = $is_collection;
            $data['video_path'] = json_decode($data['video_path'],true);
            $data['img_list'] = json_decode($data['img_list'],true);
            $data['product_details_image'] = json_decode($data['product_details_image'],true);
            $data['address_detail'] = self::getBaseStore($id)['address_detail'];
            $data['shop_name'] = self::getBaseStore($id)['company_name'];
            #规格
            $spec_data = Db::name("goods_spec")
                ->field($field_spec)
                ->where(['goods_id'=>$id])
                ->order(['price'=>"asc"])
                ->select();
            $data['spec'] = $spec_data;
            #联盟商家广告
            $advertisement_data = Db::name("goods_info")
                ->field("advertisement")
                ->where(['goods_id'=>$id])
                ->find();
            $advertisement_data =json_decode($advertisement_data['advertisement'],true)==null ? [] : json_decode($advertisement_data['advertisement'],true);
            if($advertisement_data)
            {
                foreach ($advertisement_data as $k=>$v)
                {
                    if(self::getHotGoods($v['member_id']))
                    {
                        $data["advertisement"][] = self::getHotGoods($v['member_id']);
                    }
                    if(empty($data["advertisement"]))
                    {
                        $data['advertisement'] = [];
                    }
                }

            }else{
                $data['advertisement'] = $advertisement_data;
            }
        }
        else if($goods_type == 5) {
            #商家讲坛
            $field = "s.goods_id,s.sale_total,s.member_id,s.g_name,s.cover_pic,in.video_path,   in.product_details_image,in.is_type,in.free_time";
            $field_spec = "gs_id,name,cost_price,price,proportion,gold_coin_num,num";
            $data = Db::name("goods s")
                ->alias(['mm_goods_info'=>'in'])
                ->field($field)
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where(['s.goods_id'=>$id])
                ->find();
            $member_id=get_header_id("token");
            #判断是否购买
            $is_buy = Db::name("order_goods g")
                ->join("mm_order o","g.order_id=o.order_id")
                ->where(['g.goods_id'=>$id,'o.pay_status'=>2,'o.member_id'=>$member_id])
                ->count();
            $data['is_buy'] = $is_buy == 0 ? 0 : 1;

            #商品是否收藏
            $where1['member_id'] = get_header_id("token");
            $where1['object_id'] = $id;
            $where1['class_type'] = 15;
            $count = Db::name("collection")
                ->where($where1)
                ->count();
            if($count)
            {
                $is_collection = 1;
            }else{
                $is_collection = 0;
            }
            $data['is_collection'] = $is_collection;
            $data['video_path'] = json_decode($data['video_path'],true);
            $data['product_details_image'] = json_decode($data['product_details_image'],true);
//            $data['address_detail'] = self::getBaseStore($id)['address_detail'];
            $data['shop_name'] = self::getBaseStore($id)['company_name'];
            #规格
            $spec_data = Db::name("goods_spec")
                ->field($field_spec)
                ->where(['goods_id'=>$id])
                ->order(['price'=>"asc"])
                ->select();
            foreach ($spec_data as $k=>$v)
            {
                $spec_data[$k]['buyer'] = self::getFen($v,0.5);
                $spec_data[$k]['invest'] = self::getFen($v,0.1);
                $spec_data[$k]['direct_push'] = self::getFen($v,0.1);
                $spec_data[$k]['inter_push'] = self::getFen($v,0.1);
            }
            $data['spec'] = $spec_data;
            #联盟商家广告
            $advertisement_data = Db::name("goods_info")
                ->field("advertisement")
                ->where(['goods_id'=>$id])
                ->find();
            $advertisement_data =json_decode($advertisement_data['advertisement'],true)==null ? [] : json_decode($advertisement_data['advertisement'],true);
            if($advertisement_data)
            {
                foreach ($advertisement_data as $k=>$v)
                {
                    if(self::getHotGoods($v['member_id']))
                    {
                        $data["advertisement"][] = self::getHotGoods($v['member_id']);
                    }
                    if(empty($data["advertisement"]))
                    {
                        $data['advertisement'] = [];
                    }
                }

            }else{
                $data['advertisement'] = $advertisement_data;
            }

        }
        else if($goods_type == 3){
            #返利
            $field = "s.goods_id,s.sale_total,s.member_id,in.video_path,in.img_list,s.g_name,s.limit_num_status,s.limit_time_status,s.limit_number,s.limit_start_time,s.limit_end_time,in.distribution_method,in.is_change,in.is_real,in.is_diff,in.product_details_image,in.delivery_fee";
            $field_spec = "gs_id,name,price,cost_price,num,proportion,gold_coin_num";
            $data = Db::name("goods s")
                ->alias(['mm_goods_info'=>'in'])
                ->field($field)
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where(['s.goods_id'=>$id])
                ->find();
            #商品是否收藏
            $where1['member_id'] = get_header_id("token");
            $where1['object_id'] = $id;
            $where1['class_type'] = 15;
            $count = Db::name("collection")
                ->where($where1)
                ->count();
            if($count)
            {
                $is_collection = 1;
            }else{
                $is_collection = 0;
            }
            $data['is_collection'] = $is_collection;
            $data['video_path'] = json_decode($data['video_path'],true);
            $data['img_list'] = json_decode($data['img_list'],true);
            $data['product_details_image'] = json_decode($data['product_details_image'],true);
            $data['address_detail'] = self::getBaseStore($id)['address_detail'];
            $data['shop_name'] = self::getBaseStore($id)['company_name'];
            #规格
            $spec_data = Db::name("goods_spec")
                ->field($field_spec)
                ->where(['goods_id'=>$id])
                ->order(['price'=>"asc"])
                ->select();
            foreach ($spec_data as $k=>$v)
            {
                $spec_data[$k]['buyer'] = self::getFen($v,0.5);
                $spec_data[$k]['invest'] = self::getFen($v,0.1);
                $spec_data[$k]['direct_push'] = self::getFen($v,0.1);
                $spec_data[$k]['inter_push'] = self::getFen($v,0.1);
            }
            $data['spec'] = $spec_data;
            #联盟商家广告
            $advertisement_data = Db::name("goods_info")
                ->field("advertisement")
                ->where(['goods_id'=>$id])
                ->find();
            $advertisement_data =json_decode($advertisement_data['advertisement'],true)==null ? [] : json_decode($advertisement_data['advertisement'],true);
            if($advertisement_data)
            {
                foreach ($advertisement_data as $k=>$v)
                {
                    if(self::getHotGoods($v['member_id']))
                    {
                        $data["advertisement"][] = self::getHotGoods($v['member_id']);
                    }
                    if(empty($data["advertisement"]))
                    {
                        $data['advertisement'] = [];
                    }
                }

            }else{
                $data['advertisement'] = $advertisement_data;
            }

        }
        else{
            returnJson(-2004,"商品ID异常");
        }
        return $data;
    }
    #根据比例算积分

    private function getFen($data,$score)
    {
        $return_integral = $data['price']-$data['price']*($data['proportion']/100);
        $return_integral = $return_integral * $score;
        return sprintf("%01.1f", $return_integral);
    }
    //根据商品ID获取店铺基本信息
    private function getBaseStore($id)
    {
        $data = Db::name("goods")
            ->field("store_id")
            ->where(['goods_id'=>$id])
            ->find();
        $store_id = $data['store_id'];
        $store = Db::name("store_apply")
            ->field("company_name,address_detail")
            ->where(['sa_id'=>$store_id])
            ->find();
        return $store;
    }
    //zlc-联盟商家商品收藏
    public function goodsCollection($id)
    {
        //查询商品是否为干货分享商品
        $goods=Db::name('goods')->where('goods_id',$id)->find();
        if($goods['source_type']==4){
            $data['class_type'] = 14;
            $where['class_type'] = 14;
        }else{
            $data['class_type'] = 15;
            $where['class_type'] = 15;
        }
        $data['member_id'] = get_header_id("token");
        $data['object_id'] = $id;
        $data['create_time'] = time();

        #去重
        $where['member_id'] = $data['member_id'];
        $where['object_id'] = $id;

        $count = Db::name("collection")
            ->where($where)
            ->count();
        if($count)
        {
            Db::name("collection")
                ->where($where)
                ->delete();
            returnJson(200,"取消收藏");
        }
        $c_id = Db::name("collection")
            ->insertGetId($data);
        return $c_id;

    }
    //zlc-联盟商家商铺收藏
    public function shopCollection($id)
    {
        $data['member_id'] = get_header_id("token");
        $data['object_id'] = $id;
        $data['create_time'] = time();
        $data['class_type'] = 13;
        #去重
        $where['member_id'] = $data['member_id'];
        $where['object_id'] = $id;
        $where['class_type'] = 13;
        $count = Db::name("collection")
            ->where($where)
            ->count();
        if($count)
        {
            Db::name("collection")
                ->where($where)
                ->delete();
            returnJson(200,"取消收藏");
        }
        $c_id = Db::name("collection")
            ->insertGetId($data);
        return $c_id;

    }
    //商铺首页商品展示
    public function listGoodsFirst($data)
    {
        $id = $data['id'];
        $longitude = $data['longitude'];
        $latitude = $data['latitude'];
        #查找商铺经纬度
        $distance_data = Db::name("store_apply")
            ->field("longitude,latitude,company_name")
            ->where(['sa_id'=>$id])
            ->find();
        #查看当前店铺是否收藏
        $count = Db::name("collection")
            ->where(['class_type'=>13,"object_id"=>$id])
            ->count();
        if($count)
        {
            $base['is_collection'] = 1;
        }else{
            $base['is_collection'] = 0;
        }
        $base['company_name'] = $distance_data['company_name'];

        #求出当前商铺距离自己距离
        $distance = round(get_distance($longitude,$latitude,$distance_data['longitude'],$distance_data['latitude']));
        #爆品
        $hot_data = Db::name("goods")
            ->field("goods_id as id,g_type,member_id,cover_pic,g_name,sale_total")
            ->where(['g_type'=>1,'is_delete'=>1,'status'=>1,"store_id"=>$id])
            ->order(['sale_total'=>"desc"])
            ->limit(2)
            ->select();
        foreach ($hot_data as $k=>$v)
        {
            $spec = Db::name("goods_spec")
                ->field("cost_price,price,coin_price")
                ->where(['goods_id'=>$v['id']])
                ->order(['price'=>'asc'])
                ->find();
            $hot_data[$k]['cost_price'] = $spec['cost_price'];
            $hot_data[$k]['price'] = $spec['price'];
            $hot_data[$k]['coin_price'] = $spec['coin_price'];
            $hot_data[$k]['distance'] = $distance;
        }
        #限时特惠
        $limit_goods = Db::name("goods")
            ->field("goods_id as id,g_type,member_id,cover_pic,g_name,limit_start_time,limit_end_time")
            ->where(['g_type'=>4,'is_delete'=>1,'status'=>1,"store_id"=>$id])
            ->order(['sale_total'=>"desc"])
            ->limit(2)
            ->select();
        foreach ($limit_goods as $k=>$v)
        {
            $spec = Db::name("goods_spec")
                ->field("price")
                ->where(['goods_id'=>$v['id']])
                ->order(['price'=>'asc'])
                ->find();
            $limit_goods[$k]['price'] = $spec['price'];
            $limit_goods[$k]['company_name'] = $distance_data['company_name'];
            $limit_goods[$k]['distance'] = $distance;
        }
        #裂变商品
        $change_goods = Db::name("goods")
            ->field("goods_id as id,g_type,member_id,cover_pic,g_name")
            ->where(['g_type'=>2,'is_delete'=>1,'status'=>1,"store_id"=>$id])
            ->order(['sale_total'=>"desc"])
            ->limit(2)
            ->select();
        foreach ($change_goods as $k=>$v)
        {
            $info = Db::name("goods_info")
                ->field("copper_pop,silver_pop,gold_pop")
                ->where(['goods_id'=>$v['id']])
                ->find();
            $spec1 = Db::name("goods_spec")
                ->field("price")
                ->where(['goods_id'=>$v['id']])
                ->order(['price'=>'asc'])
                ->find();

            $change_goods[$k]['price'] = $spec1['price'];
            $change_goods[$k]['copper_pop'] = $info['copper_pop'];
            $change_goods[$k]['silver_pop'] = $info['silver_pop'];
            $change_goods[$k]['gold_pop'] = $info['gold_pop'];
            $change_goods[$k]['company_name'] = $distance_data['company_name'];
            $change_goods[$k]['distance'] = $distance;
        }
        return compact("base","hot_data","limit_goods","change_goods");

    }
    //zlc-商铺首页分类--商家返利
    public function listGoodsRebateCate($data,$current_page,$page_num)
    {
        $id = $data['id'];
        $longitude = $data['longitude'];
        $latitude = $data['latitude'];

        #查找商铺经纬度
        $distance_data = Db::name("store_apply")
            ->field("longitude,latitude,company_name")
            ->where(['sa_id'=>$id])
            ->find();
        #查看当前店铺是否收藏
        $count = Db::name("collection")
            ->where(['class_type'=>13,"object_id"=>$id])
            ->count();
        if($count)
        {
            $base['is_collection'] = 1;
        }else{
            $base['is_collection'] = 0;
        }
        $where['g_type'] = 3;
        $where['is_delete'] = 1;
        $where['status'] = 1;
        $where['store_id'] = $id;
        if(!empty($data['type']))
        {
            $where['gc_id'] = $data['type'];
        }

        #求出当前商铺距离自己距离
        $distance = round(get_distance($longitude,$latitude,$distance_data['longitude'],$distance_data['latitude']));
        $forum = Db::name("goods s")
            ->alias(['mm_goods_info'=>"in"])
            ->field("s.goods_id as id,s.g_type,s.member_id,s.cover_pic,s.g_name,s.sale_total,in.is_type")
            ->where($where)
            ->join("mm_goods_info","in.goods_id = s.goods_id")
            ->order(['s.sale_total'=>"desc"])
            ->page($current_page,$page_num)
            ->select();
        foreach ($forum as $k=>$v)
        {
            $spec = Db::name("goods_spec")
                ->field("cost_price,price,gold_coin_num,proportion")
                ->where(['goods_id'=>$v['id']])
                ->order(['price'=>'asc'])
                ->find();
            $forum[$k]['distance'] = $distance;
            $forum[$k]['cost_price'] = $spec['cost_price'];
            $forum[$k]['price'] = $spec['price'];
            $forum[$k]['gold_coin_num'] = $spec['gold_coin_num'];
            $return_integral = ($spec['price']-$spec['price']*($spec['proportion']/100))*0.5;
            $forum[$k]['return_integral'] = sprintf("%01.1f", $return_integral);
        }
        $total_num = Db::name("goods s")
            ->alias(['mm_goods_info'=>"in"])
            ->where($where)
            ->join("mm_goods_info","in.goods_id = s.goods_id")
            ->count();

        $list['company_name'] = $distance_data['company_name'];
        $list['current_page'] = $current_page;
        $list['total_page'] = ceil($total_num / $page_num);
        $list['page_num'] = $page_num;
        $list['total_num'] = $total_num;
        $list['data'] = $forum;
        return $list;
    }
    //zlc-商铺首页展示--商家返利
    public function listGoodsRebate($data)
    {
        $id = $data['id'];
        $longitude = $data['longitude'];
        $latitude = $data['latitude'];

        #查找商铺经纬度
        $distance_data = Db::name("store_apply")
            ->field("longitude,latitude,company_name")
            ->where(['sa_id'=>$id])
            ->find();
        #查看当前店铺是否收藏
        $count = Db::name("collection")
            ->where(['class_type'=>13,"object_id"=>$id])
            ->count();
        if($count)
        {
            $base['is_collection'] = 1;
        }else{
            $base['is_collection'] = 0;
        }
        $where['g_type'] = 3;
        $where['is_delete'] = 1;
        $where['status'] = 1;
        $where['store_id'] = $id;
        if(!empty($data['type']))
        {
            $where['gc_id'] = $data['type'];
        }

        #求出当前商铺距离自己距离
        $distance = round(get_distance($longitude,$latitude,$distance_data['longitude'],$distance_data['latitude']));
        $forum = Db::name("goods s")
            ->alias(['mm_goods_info'=>"in"])
            ->field("s.goods_id as id,s.g_type,s.member_id,s.cover_pic,s.g_name,s.sale_total,in.is_type")
            ->where($where)
            ->join("mm_goods_info","in.goods_id = s.goods_id")
            ->order(['s.sale_total'=>"desc"])
            ->limit(2)
            ->select();
        foreach ($forum as $k=>$v)
        {
            $spec = Db::name("goods_spec")
                ->field("cost_price,price,gold_coin_num,proportion")
                ->where(['goods_id'=>$v['id']])
                ->order(['price'=>'asc'])
                ->find();
            $forum[$k]['distance'] = $distance;
            $forum[$k]['company_name'] = $distance_data['company_name'];
            $forum[$k]['cost_price'] = $spec['cost_price'];
            $forum[$k]['price'] = $spec['price'];
            $forum[$k]['gold_coin_num'] = $spec['gold_coin_num'];
            $return_integral = ($spec['price']-$spec['price']*($spec['proportion']/100))*0.5;
            $forum[$k]['return_integral'] = sprintf("%01.1f", $return_integral);
        }
        return $forum;



    }
    //zlc-商铺首页展示--商家讲坛
    public function listGoodsForum($data)
    {
        $id = $data['id'];
        $longitude = $data['longitude'];
        $latitude = $data['latitude'];
        $type = $data['type'];
        #查找商铺经纬度
        $distance_data = Db::name("store_apply")
            ->field("longitude,latitude,company_name")
            ->where(['sa_id'=>$id])
            ->find();
        #查看当前店铺是否收藏
        $count = Db::name("collection")
            ->where(['class_type'=>13,"object_id"=>$id])
            ->count();
        if($count)
        {
            $base['is_collection'] = 1;
        }else{
            $base['is_collection'] = 0;
        }

        #求出当前商铺距离自己距离
        $distance = round(get_distance($longitude,$latitude,$distance_data['longitude'],$distance_data['latitude']));
        $forum = Db::name("goods s")
            ->alias(['mm_goods_info'=>"in"])
            ->field("s.goods_id as id,s.g_type,s.member_id,s.cover_pic,s.g_name,s.sale_total,in.is_type")
            ->where(['s.g_type'=>5,'s.is_delete'=>1,'s.status'=>1,"s.store_id"=>$id,"in.is_type"=>$type])
            ->join("mm_goods_info","in.goods_id = s.goods_id")
            ->order(['s.sale_total'=>"desc"])
            ->limit(2)
            ->select();
        foreach ($forum as $k=>$v)
        {
            $spec = Db::name("goods_spec")
                ->field("cost_price,price,gold_coin_num,proportion")
                ->where(['goods_id'=>$v['id']])
                ->order(['price'=>'asc'])
                ->find();
            $forum[$k]['distance'] = $distance;
            $forum[$k]['company_name'] = $distance_data['company_name'];
            $forum[$k]['cost_price'] = $spec['cost_price'];
            $forum[$k]['price'] = $spec['price'];
            $forum[$k]['gold_coin_num'] = $spec['gold_coin_num'];
            $return_integral = ($spec['price']-$spec['price']*($spec['proportion']/100))*0.5;
            $forum[$k]['return_integral'] = sprintf("%01.1f", $return_integral);
        }
        return $forum;



    }
    //zlc-商铺首页分类--商家讲坛
    public function listGoodsForumCate($data,$current_page,$page_num)
    {
        $id = $data['id'];
        $longitude = $data['longitude'];
        $latitude = $data['latitude'];
        $type = $data['type'];
        #查找商铺经纬度
        $distance_data = Db::name("store_apply")
            ->field("longitude,latitude,company_name")
            ->where(['sa_id'=>$id])
            ->find();
        #查看当前店铺是否收藏
        $count = Db::name("collection")
            ->where(['class_type'=>13,"object_id"=>$id])
            ->count();
        if($count)
        {
            $base['is_collection'] = 1;
        }else{
            $base['is_collection'] = 0;
        }

        #求出当前商铺距离自己距离
        $distance = round(get_distance($longitude,$latitude,$distance_data['longitude'],$distance_data['latitude']));
        $forum = Db::name("goods s")
            ->alias(['mm_goods_info'=>"in"])
            ->field("s.goods_id as id,s.g_type,s.member_id,s.cover_pic,s.g_name,s.sale_total,in.is_type")
            ->where(['s.g_type'=>5,'s.is_delete'=>1,'s.status'=>1,"s.store_id"=>$id,"in.is_type"=>$type])
            ->join("mm_goods_info","in.goods_id = s.goods_id")
            ->order(['s.sale_total'=>"desc"])
            ->page($current_page,$page_num)
            ->select();
        foreach ($forum as $k=>$v)
        {
            $spec = Db::name("goods_spec")
                ->field("cost_price,price,gold_coin_num,proportion")
                ->where(['goods_id'=>$v['id']])
                ->order(['price'=>'asc'])
                ->find();
            $forum[$k]['distance'] = $distance;
            $forum[$k]['cost_price'] = $spec['cost_price'];
            $forum[$k]['price'] = $spec['price'];
            $forum[$k]['gold_coin_num'] = $spec['gold_coin_num'];
            $return_integral = ($spec['price']-$spec['price']*($spec['proportion']/100))*0.5;
            $forum[$k]['return_integral'] = sprintf("%01.1f", $return_integral);
        }
        $total_num = Db::name("goods s")
            ->alias(['mm_goods_info'=>"in"])
            ->where(['s.g_type'=>5,'s.is_delete'=>1,'s.status'=>1,"s.store_id"=>$id,"in.is_type"=>$type])
            ->join("mm_goods_info","in.goods_id = s.goods_id")
            ->page($current_page,$page_num)
            ->count();
        $list['company_name'] = $distance_data['company_name'];
        $list['current_page'] = $current_page;
        $list['total_page'] = ceil($total_num / $page_num);
        $list['page_num'] = $page_num;
        $list['total_num'] = $total_num;
        $list['data'] = $forum;
        return $list;

    }
    //商铺首页备注
    public function storeNote()
    {
        $data = Db::name("store_note")
            ->field("id,content,pop")
            ->select();
        foreach ($data as $k=>$v)
        {
            $data[$k]['pop'] =  $data[$k]['pop']==null ? "" : $data[$k]['pop'];
        }
        return $data;
    }
    //商铺首页轮播
    public function shopsPic($id)
    {
        $data = Db::name("store_detail")
            ->field("img_list,video_path")
            ->where(['sa_id'=>$id])
            ->find();
        $store = Db::name("store_apply")
            ->field("company_name")
            ->where(['sa_id'=>$id])
            ->find();
        $data['shop_name'] = $store['company_name'];
        $data['img_list'] = json_decode($data['img_list'],true);
        $data['video_path'] = json_decode($data['video_path'],true);
        return $data;
    }
    //zlc-根据行业分类查找店铺
    public function storeList($data)
    {
        if(!empty($data['company_name']))
        {
            $company_name = $data['company_name'];
            $where['company_name'] = ["like","%$company_name%"];
        }
        #分类条件
        $where['ic_id'] = $data['type'];
//        $where['apply_status'] = 1; #店铺开启状态
        $longitude = $data['longitude'];
        $latitude = $data['latitude'];
        $data = Db::name("store_apply a")
            ->field("a.sa_id as id,a.company_name,a.introduce,a.longitude,a.latitude,a.apply_status,a.score,a.label")
            ->where($where)
            ->select();
        foreach ($data as $k=>$v)
        {
            $data[$k]['label'] = json_decode($v['label'],true)==null ? [] : json_decode($v['label'],true);
            $data[$k]['distance'] = round(get_distance($longitude,$latitude,$v['longitude'],$v['latitude']));
            unset($data[$k]['longitude']);
            unset($data[$k]['latitude']);
            $img_data = Db::name("store_detail")
                ->field("img_list")
                ->where(['sa_id'=>$v['id']])
                ->find();
            $img_data = json_decode($img_data['img_list'],true);
            $data[$k]['img'] = $img_data[0];
        }

        $last_names = array_column($data,'distance');
        array_multisort($last_names,SORT_ASC ,$data);
        return $data;
    }
    //首页底部和顶部轮播
    public function advert($type)
    {
        if($type == 1)
        {
            $data = Db::name("advert")
                ->field("jump_type,advert_pic as img_address,jump_id,target_url as jump_url")
                ->where(['status'=>1,'type'=>1,'location'=>1])
                ->order(['create_time'=>'desc'])
                ->select();

            $data2 = Db::name("advert")
                ->field("jump_type,advert_pic as img_address,jump_id,target_url as jump_url")
                ->where(['status'=>1,'type'=>1,'location'=>2])
                ->order(['create_time'=>'desc'])
                ->select();


            $list['advert_top']=$data;
            $list['advert_bottom']=$data2;
        }else if($type == 2)
        {
            $data2 = Db::name("advert")
                ->field("jump_type,advert_pic as img_address,jump_id,target_url as jump_url")
                ->where(['status'=>1,'type'=>2,'location'=>2])
                ->order(['create_time'=>'desc'])
                ->select();

            $data = Db::name("advert")
                ->field("jump_type,advert_pic as img_address,jump_id,target_url as jump_url")
                ->where(['status'=>1,'type'=>2,'location'=>1])
                ->order(['create_time'=>'desc'])
                ->select();

            $list['advert_top']=$data;
            $list['advert_bottom']=$data2;
        }else if($type == 4)
        {
            $data2 = Db::name("advert")
                ->field("jump_type,advert_pic as img_address,jump_id,target_url as jump_url")
                ->where(['status'=>1,'type'=>4,'location'=>2])
                ->order(['create_time'=>'desc'])
                ->limit(5)
                ->select();

            $data = Db::name("advert")
                ->field("jump_type,advert_pic as img_address,jump_id,target_url as jump_url")
                ->where(['status'=>1,'type'=>2,'location'=>1])
                ->order(['create_time'=>'desc'])
                ->select();

            $list['advert_top']=$data;
            $list['advert_bottom']=$data2;
        }else if($type == 5)
        {
            $data2 = Db::name("advert")
                ->field("jump_type,advert_pic as img_address,jump_id,target_url as jump_url")
                ->where(['status'=>1,'type'=>5,'location'=>2])
                ->order(['create_time'=>'desc'])
                ->limit(5)
                ->select();

            $data = Db::name("advert")
                ->field("jump_type,advert_pic as img_address,jump_id,target_url as jump_url")
                ->where(['status'=>1,'type'=>5,'location'=>1])
                ->order(['create_time'=>'desc'])
                ->select();
            $list['advert_top']=$data;
            $list['advert_bottom']=$data2;
        }
        return $list;
    }
    //公告
    public function notice()
    {
        $data = Db::name("notice")
            ->field("content")
            ->where(['status'=>1])
            ->order(['create_time'=>"desc"])
            ->find();
        return $data;
    }
    //whetherShop
    public function whetherShop($tel)
    {
        $data = Db::name("store_apply")
            ->field("member_id,company_name")
            ->where(['phone'=>$tel])
            ->find();
        if($data === NULL){
            returnJson(-2004,"商家不存在");
        }
        $member_id = $data['member_id'];
        $goods = self::getHotGoods($member_id);
        if($goods == null)
        {
            returnJson(-2004,"该商家尚未设置店招爆品");
        }
        $list['member_id'] = $member_id;
        $list['img'] = $goods['cover_pic'];
        $list['company_name'] = $data['company_name'];
        return $list;
    }
    public function pointsAwardConfig()
    {
        $data = Db::name("system_config")
            ->field("field_value")
            ->where(['field_name'=>"points_award_config"])
            ->find();
        return json_decode($data['field_value'],true);
    }
    //消费爆品管理查看详情
    public function explosiveShowDetail($id)
    {
        $type = self::getType($id);
        if($type == 1)
        {
            $field_spec = "gs_id,name,price,cost_price,num,coin_price";
            $field = "s.goods_id,s.store_id,s.g_name,s.status,in.video_path,in.img_list,s.limit_num_status,s.limit_number,s.limit_time_status,s.limit_start_time,s.limit_end_time,in.distribution_method,in.delivery_type,in.delivery_fee,in.is_change,in.is_real,in.is_diff,in.product_details_image,in.advertisement";
        }else if($type == 2){
            $field_spec = "gs_id,name,price,cost_price,num";
            $field = "s.goods_id,s.store_id,s.g_name,s.status,in.video_path,in.img_list,s.limit_num_status,s.limit_number,s.limit_time_status,s.limit_start_time,s.limit_end_time,in.distribution_method,in.delivery_type,in.delivery_fee,in.product_details_image,in.advertisement,in.copper_money,in.copper_pop,in.silver_money,in.silver_pop,in.gold_money,in.gold_pop";
        }else if($type == 4)
        {
            $field_spec = "gs_id,cost_price,price as registration_fee";
            $field = "s.goods_id,s.store_id,s.cover_pic,s.g_name,s.status,in.video_path,in.img_list,in.distribution_method,in.delivery_type,in.delivery_fee,in.is_change,in.is_real,in.is_diff,in.product_details_image,in.advertisement,in.free_gift,in.consumption_pic,in.direct_push,in.inter_push,s.limit_number as limit_registration_num,s.limit_start_time as activity_start_time,s.limit_end_time as activity_end_time";
        }else if($type == 5)
        {
            $field_spec = "gs_id,cost_price,price,gold_coin_num,proportion";
            $field = "s.goods_id,s.store_id,s.g_name,s.cover_pic,s.status,in.video_path,in.img_list,in.free_time,in.product_details_image,in.advertisement,in.is_type";
        }else if($type == 3)
        {
            $field_spec = "gs_id,name,cost_price,price,num,proportion,gold_coin_num";
            $field = "s.goods_id,s.gc_id,s.store_id,s.cover_pic,s.g_name,s.status,in.video_path,in.img_list,in.distribution_method,in.delivery_type,in.delivery_fee,in.is_change,in.is_real,in.is_diff,in.product_details_image,in.advertisement,s.limit_num_status,s.limit_number,s.limit_time_status,s.limit_start_time,s.limit_end_time";
        }
        else{
            returnJson(-2004,"商品ID异常");
        }

        $data = Db::name("goods s")
            ->field($field)
            ->alias(['mm_goods_info'=>'in'])
            ->join("mm_goods_info","in.goods_id = s.goods_id")
            ->where(['s.goods_id'=>$id])
            ->find();
        $data['company_name'] = self::getName($data['store_id']);
        $data['video_path'] = json_decode($data['video_path'],true);
        $data['img_list'] = json_decode($data['img_list'],true);
        $data['product_details_image'] = json_decode($data['product_details_image'],true);
//        $data['points_award'] = self::pointsAwardConfig();
        $data['advertisement'] = json_decode($data['advertisement'],true);

        $data['specifications'] = Db::name("goods_spec")
            ->field($field_spec)
            ->where(['goods_id'=>$id])
            ->select();
        return $data;
    }
    //通过店铺id店铺名称
    private function getName($id)
    {
        $data = Db::name("store_apply")
            ->field("company_name")
            ->where(['sa_id'=>$id])
            ->find();
        return $data['company_name'];
    }
    //查询商品类型$id 商品id
    private function getType($id)
    {
        $data = Db::name("goods")
            ->field("g_type")
            ->where(['goods_id'=>$id])
            ->find();
        return $data['g_type'];
    }

    //下架商品的删除
    public function shopDel($id)
    {
        $count = Db::name("goods")
            ->where(['goods_id'=>$id,'status'=>1,'is_delete'=>1])
            ->count();
        if($count)
        {
            returnJson(-2004,"已上架商品不能删除");
        }
        #判断店招商品且时间未过期不能删除
        $data = Db::name("goods_shop_sign")
            ->field("end_time")
            ->where(['goods_id'=>$id,'is_delete'=>1])
            ->find();
        if($data)
        {
            if(time()<intval($data['end_time']))
            {
                returnJson(-2004,"店招商品未过期,不能删除");
            }
        }
        Db::name("goods")
            ->where(['goods_id'=>$id])
            ->update(['is_delete'=>2]);

    }
    //商品上架或者下架,使用所有商品的上下架
    public function isShow($data)
    {
        $where['goods_id'] = $data['goods_id'];
        if($data['status'] == 1)
        {
            $list['status'] = 2;
            $msg = "商品下架成功";
        }else{
            $list['status'] = 1;
            $msg = "商品上架成功";
        }
        Db::name("goods")
            ->where($where)
            ->update($list);
        return $msg;
    }

    public function explosiveList($current_page,$page_num,$data)
    {
        $id = $data['id'];
        $type = $data['type'];
        $longitude = $data['longitude'];
        $latitude = $data['latitude'];
        #查找商铺经纬度
        $distance_data = Db::name("store_apply")
            ->field("longitude,latitude,company_name")
            ->where(['sa_id'=>$id])
            ->find();

        #求出当前商铺距离自己距离
        $distance = round(get_distance($longitude,$latitude,$distance_data['longitude'],$distance_data['latitude']));
        #消费爆品
        if($type==1)
        {
            $data = Db::name("goods s")
                ->field("s.goods_id as id,s.g_type,member_id,s.cover_pic,s.g_name,s.sale_total")
                ->where(['s.g_type'=>1,'s.is_delete'=>1,'s.status'=>1,"s.store_id"=>$id])
                ->order(['s.sale_total'=>"desc"])
                ->page($current_page,$page_num)
                ->select();
            foreach ($data as $k=>$v)
            {
                $spec = Db::name("goods_spec")
                    ->field("cost_price,price,coin_price")
                    ->where(['goods_id'=>$v['id']])
                    ->order(['price'=>'asc'])
                    ->find();
                $data[$k]['cost_price'] = $spec['cost_price'];
                $data[$k]['price'] = $spec['price'];
                $data[$k]['coin_price'] = $spec['coin_price'];
                $data[$k]['distance'] = $distance;
            }
            $total_num = Db::name("goods")
                ->where(['g_type'=>1,'is_delete'=>1,'status'=>1])
                ->count();

        }else if($type==4)
        {
            $data = Db::name("goods")
                ->field("goods_id as id,g_type,member_id,cover_pic,g_name,limit_start_time,limit_end_time")
                ->where(['g_type'=>4,'is_delete'=>1,'status'=>1,"store_id"=>$id])
                ->order(['sale_total'=>"desc"])
                ->page($current_page,$page_num)
                ->select();
            foreach ($data as $k=>$v)
            {
                $spec = Db::name("goods_spec")
                    ->field("price")
                    ->where(['goods_id'=>$v['id']])
                    ->order(['price'=>'asc'])
                    ->find();
                $data[$k]['price'] = $spec['price'];
                $data[$k]['company_name'] = $distance_data['company_name'];
                $data[$k]['distance'] = $distance;
            }
            $total_num = Db::name("goods")
                ->where(['g_type'=>4,'is_delete'=>1,'status'=>1])
                ->count();
        }else if($type==2)
        {
            $data = Db::name("goods")
                ->field("goods_id as id,g_type,member_id,cover_pic,g_name")
                ->where(['g_type'=>2,'is_delete'=>1,'status'=>1,"store_id"=>$id])
                ->order(['sale_total'=>"desc"])
                ->page($current_page,$page_num)
                ->select();
            foreach ($data as $k=>$v)
            {
                $info = Db::name("goods_info")
                    ->field("copper_pop,silver_pop,gold_pop")
                    ->where(['goods_id'=>$v['id']])
                    ->find();
                $spec1 = Db::name("goods_spec")
                    ->field("price")
                    ->where(['goods_id'=>$v['id']])
                    ->order(['price'=>'asc'])
                    ->find();

                $data[$k]['price'] = $spec1['price'];
                $data[$k]['copper_pop'] = $info['copper_pop'];
                $data[$k]['silver_pop'] = $info['silver_pop'];
                $data[$k]['gold_pop'] = $info['gold_pop'];
                $data[$k]['company_name'] = $distance_data['company_name'];
                $data[$k]['distance'] = $distance;
            }
            $total_num = Db::name("goods")
                ->where(['g_type'=>2,'is_delete'=>1,'status'=>1])
                ->count();
        }

        $list['company_name'] = $distance_data['company_name'];
        $list['current_page'] = $current_page;
        $list['total_page'] = ceil($total_num / $page_num);
        $list['page_num'] = $page_num;
        $list['total_num'] = $total_num;
        $list['data'] = $data;
        return $list;

    }
    public function goodsClass()
    {
        $data = [
            ['id'=>1,'name'=>'全部商品'],
            ['id'=>2,'name'=>'消费爆品'],
            ['id'=>3,'name'=>'返利商品'],
            ['id'=>4,'name'=>'限时特惠'],
            ['id'=>5,'name'=>'裂变爆品'],
            ['id'=>6,'name'=>'商家讲坛'],
        ];
        return $data;
    }
    #判断是否是店招商品
    private function isRecommend($id)
    {
        $count = Db::name("goods_shop_sign")
            ->field("is_free,start_time,end_time")
            ->where(['goods_id'=>$id,"is_delete"=>1])
            ->find();
        if($count)
        {
            if($count['end_time'] < time() && $count['is_free'] ==2 )
            {
                $is_recommend = [];
            }else{
                $is_recommend["is_free"] = $count['is_free'];
                $is_recommend["start_time"] = $count['start_time'];
                $is_recommend["end_time"] = $count['end_time'];
            }

        }else{
            $is_recommend = [];
        }
        return $is_recommend;
    }

    //消费爆品管理展示
    public function explosiveShow($current_page,$page_num,$where,$type,$is_type)
    {
        $where['member_id'] = get_header_id("token");
        #type=1 消费爆品
        if($type == 1)
        {

            $where['g_type'] = 1;
            $where['is_delete'] = 1;
            $data = Db::name("goods")
                ->field("goods_id,cover_pic,g_name,status,store_id")
                ->where($where)
                ->order(['create_time'=>'desc'])
                ->page($current_page,$page_num)
                ->select();
            foreach ($data as $k=>$v)
            {

                $array = Db::name("goods_spec")
                    ->field("price,cost_price,coin_price")
                    ->where(['goods_id'=>$v['goods_id']])
                    ->order(['price'=>'asc'])
                    ->find();
                $data[$k]['is_recommend'] = self::isRecommend($v['goods_id']);
                $data[$k]['company_name'] = self::getName($v['store_id']);
                $data[$k]['cost_price'] = $array['cost_price'];
                $data[$k]['price'] = $array['price'];
                $data[$k]['coin_price'] = $array['coin_price'];
            }
            $total_num = Db::name("goods")
                ->where($where)
                ->count();
        }else if($type == 2)
        {
            #type=2 裂变商品
//            $where['member_id'] = get_header_id("token");
            $where['g_type'] = 2;
            $where['is_delete'] = 1;
            $data = Db::name("goods s")
                ->alias(['mm_goods_info'=>"in"])
                ->field("s.goods_id,s.store_id,s.cover_pic,s.g_name,s.status,in.copper_pop,in.silver_pop,in.gold_pop")
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where($where)
                ->order(['s.create_time'=>'desc'])
                ->page($current_page,$page_num)
                ->select();
            foreach ($data as $k=>$v)
            {
                $array = Db::name("goods_spec")
                    ->field("price,cost_price")
                    ->where(['goods_id'=>$v['goods_id']])
                    ->order(['price'=>'asc'])
                    ->find();
                $data[$k]['is_recommend'] = self::isRecommend($v['goods_id']);
                $data[$k]['company_name'] = self::getName($v['store_id']);
                $data[$k]['cost_price'] = $array['cost_price'];
                $data[$k]['price'] = $array['price'];
            }
            $total_num = Db::name("goods")
                ->where($where)
                ->count();
        }else if($type == 4)
        {
//            $where['member_id'] = get_header_id("token");
            $where['g_type'] = 4;
            $where['is_delete'] = 1;
            $data = Db::name("goods s")
                ->alias(['mm_goods_info'=>"in"])
                ->field("s.goods_id,s.store_id,s.cover_pic,s.g_name,s.status,s.limit_start_time as activity_start_time,s.limit_end_time as activity_end_time")
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where($where)
                ->order(['s.create_time'=>'desc'])
                ->page($current_page,$page_num)
                ->select();
            foreach ($data as $k=>$v)
            {
                $array = Db::name("goods_spec")
                    ->field("price as registration_fee")
                    ->where(['goods_id'=>$v['goods_id']])
                    ->order(['registration_fee'=>'asc'])
                    ->find();
                $data[$k]['is_recommend'] = self::isRecommend($v['goods_id']);
                $data[$k]['company_name'] = self::getName($v['store_id']);
                $data[$k]['registration_fee'] = $array['registration_fee'];
            }
            $total_num = Db::name("goods")
                ->where($where)
                ->count();

        }else if($type == 5)
        {
//            $where['member_id'] = get_header_id("token");
            $where['g_type'] = 5;
            $where['is_delete'] = 1;
            $where['is_type'] = $is_type;
            $data = Db::name("goods s")
                ->alias(['mm_goods_info'=>"in"])
                ->field("s.goods_id,s.store_id,s.cover_pic,s.g_name,s.status,in.video_path,in.is_type")
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where($where)
                ->order(['s.create_time'=>'desc'])
                ->page($current_page,$page_num)
                ->select();
            foreach ($data as $k=>$v)
            {
                $array = Db::name("goods_spec")
                    ->field("price,cost_price,proportion,gold_coin_num")
                    ->where(['goods_id'=>$v['goods_id']])
                    ->order(['price'=>'asc'])
                    ->find();
                $data[$k]['is_recommend'] = self::isRecommend($v['goods_id']);
                $data[$k]['company_name'] = self::getName($v['store_id']);
                $data[$k]['price'] = $array['price'];
                $data[$k]['cost_price'] = $array['cost_price'];
                $data[$k]['proportion'] = $array['proportion'];
                $data[$k]['gold_coin_num'] = $array['gold_coin_num'];
                $data[$k]['video_path'] = json_decode($v['video_path'],true);
                $return_integral = ($array['price']-$array['price']*($array['proportion']/100))*0.5;
                $data[$k]['return_integral'] = sprintf("%01.1f", $return_integral);
//                $data[$k]['points_award'] = self::pointsAwardConfig();
            }
            $total_num = Db::name("goods s")
                ->alias(['mm_goods_info'=>"in"])
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where($where)
                ->count();

        }else if($type == 3)
        {
//            $where['member_id'] = get_header_id("token");
            $where['g_type'] = 3;
            $where['is_delete'] = 1;
            if(!empty($is_type))
            {
                $where['gc_id'] = $is_type;
            }
            $data = Db::name("goods s")
                ->alias(['mm_goods_info'=>"in"])
                ->field("s.goods_id,s.store_id,s.cover_pic,s.g_name,s.status")
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where($where)
                ->order(['s.create_time'=>'desc'])
                ->page($current_page,$page_num)
                ->select();
            foreach ($data as $k=>$v)
            {
                $array = Db::name("goods_spec")
                    ->field("price,cost_price,proportion,gold_coin_num")
                    ->where(['goods_id'=>$v['goods_id']])
                    ->order(['price'=>'asc'])
                    ->find();
                $data[$k]['is_recommend'] = self::isRecommend($v['goods_id']);
                $data[$k]['company_name'] = self::getName($v['store_id']);
                $data[$k]['price'] = $array['price'];
                $data[$k]['cost_price'] = $array['cost_price'];
                $data[$k]['proportion'] = $array['proportion'];
                $data[$k]['gold_coin_num'] = $array['gold_coin_num'];
                $return_integral = ($array['price']-$array['price']*($array['proportion']/100))*0.5;
                $data[$k]['return_integral'] = sprintf("%01.1f", $return_integral);
//                $data[$k]['points_award'] = self::pointsAwardConfig();
            }
            $total_num = Db::name("goods s")
                ->alias(['mm_goods_info'=>"in"])
                ->join("mm_goods_info","in.goods_id = s.goods_id")
                ->where($where)
                ->count();

        }

        $list['current_page'] = $current_page;
        $list['total_page'] = ceil($total_num / $page_num);
        $list['page_num'] = $page_num;
        $list['total_num'] = $total_num;
        $list['data'] = $data;
        return $list;
    }
    #返利商品发布和编辑
    public function rebateSave($data)
    {
        Db::startTrans();
        try{
            if(empty($data['goods_id']))
            {
                #判断添加的唯一性, 用商品名称判断
                self::isOnly(['g_name'=>$data['g_name'],'g_type'=>3]);
                //商品表
                $goods['gc_id'] = empty($data['gc_id']) ? 0 : $data['gc_id'];
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
                $goods['member_id'] = $data['member_id'];
                $goods['status'] = 1;
                $goods['source_type'] = 1;
                $goods['g_type'] = 3;
                $goods['create_time'] = time();
                $goods['store_id'] = self::getSaId($data['member_id']);
                $goods['limit_number'] = empty($data['limit_number']) ? 0 : $data['limit_number'];
                $goods['limit_num_status'] = empty($data['limit_num_status']) ? 0 : $data['limit_num_status'];
                $goods['limit_time_status'] = empty($data['limit_time_status']) ? 0 : $data['limit_time_status'];
                $goods['limit_start_time'] = empty($data['limit_start_time']) ? 0 : $data['limit_start_time'];
                $goods['limit_end_time'] = empty($data['limit_end_time']) ? 0 : $data['limit_end_time'];
                $goods_id = Db::name("goods")
                    ->insertGetId($goods);

                //详情表
                $detail['goods_id'] = $goods_id;
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
//                $detail['points_award'] = empty($data['points_award']) ? "" : $data['points_award'];
//                $detail['is_gold_coin'] = empty($data['is_gold_coin']) ? 0 : $data['is_gold_coin'];
//                $detail['gold_coin_num'] = empty($data['gold_coin_num']) ? 0 : $data['gold_coin_num'];
                $detail['distribution_method'] = empty($data['distribution_method']) ? "" : $data['distribution_method'];
                $detail['delivery_type'] = empty($data['delivery_type']) ? 0 : $data['delivery_type'];
                $detail['delivery_fee'] = empty($data['delivery_fee']) ? 0 : $data['delivery_fee'];
                $detail['is_real'] = empty($data['is_real']) ? 0 : $data['is_real'];
                $detail['is_diff'] = empty($data['is_diff']) ? 0 : $data['is_diff'];
                $detail['is_change'] = empty($data['is_change']) ? 0 : $data['is_change'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];

                Db::name("goods_info")->insert($detail);

                //规格表
                $specifications = json_decode($data['specifications'],true);
                foreach ($specifications as $k=>$v)
                {
                    $spec['goods_id'] = $goods_id;
                    $spec['name'] = $v['name'];
                    $spec['price'] = $v['price'];
                    $spec['cost_price'] = $v['cost_price'];
                    $spec['proportion'] = $v['proportion'];
                    $spec['gold_coin_num'] = $v['gold_coin_num'];
                    $spec['num'] = $v['num'];
                    Db::name("goods_spec")->insert($spec);
                }

            }else{
                #判断添加的唯一性, 用商品名称判断
                $g_name = $data['g_name'];
                $goods_id = $data['goods_id'];
                $g_type = 3;
                $where = "g_name = '$g_name' and g_type = '$g_type' and goods_id != $goods_id";
                self::isOnly($where);
                //商品表
                $goods['gc_id'] = empty($data['gc_id']) ? 0 : $data['gc_id'];
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
                $goods['member_id'] = $data['member_id'];
                $goods['update_time'] = time();
                $goods['limit_number'] = empty($data['limit_number']) ? 0 : $data['limit_number'];
                $goods['limit_num_status'] = empty($data['limit_num_status']) ? 0 : $data['limit_num_status'];
                $goods['limit_time_status'] = empty($data['limit_time_status']) ? 0 : $data['limit_time_status'];
                $goods['limit_start_time'] = empty($data['limit_start_time']) ? 0 : $data['limit_start_time'];
                $goods['limit_end_time'] = empty($data['limit_end_time']) ? 0 : $data['limit_end_time'];
                Db::name("goods")
                    ->where(['goods_id'=>$goods_id])
                    ->update($goods);

                //详情表
                $detail['goods_id'] = $goods_id;
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
//                $detail['points_award'] = empty($data['points_award']) ? "" : $data['points_award'];
//                $detail['is_gold_coin'] = empty($data['is_gold_coin']) ? 0 : $data['is_gold_coin'];
//                $detail['gold_coin_num'] = empty($data['gold_coin_num']) ? 0 : $data['gold_coin_num'];
                $detail['distribution_method'] = empty($data['distribution_method']) ? "" : $data['distribution_method'];
                $detail['delivery_type'] = empty($data['delivery_type']) ? 0 : $data['delivery_type'];
                $detail['delivery_fee'] = empty($data['delivery_fee']) ? 0 : $data['delivery_fee'];
                $detail['is_real'] = empty($data['is_real']) ? 0 : $data['is_real'];
                $detail['is_diff'] = empty($data['is_diff']) ? 0 : $data['is_diff'];
                $detail['is_change'] = empty($data['is_change']) ? 0 : $data['is_change'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];

                Db::name("goods_info")
                    ->where(['goods_id'=>$goods_id])
                    ->update($detail);

                //规格表
                $specifications = json_decode($data['specifications'],true);
                Db::name("goods_spec")->where(['goods_id'=>$goods_id])->delete();
                foreach ($specifications as $k=>$v)
                {
                    $spec['goods_id'] = $goods_id;
                    $spec['name'] = $v['name'];
                    $spec['price'] = $v['price'];
                    $spec['cost_price'] = $v['cost_price'];
                    $spec['proportion'] = $v['proportion'];
                    $spec['gold_coin_num'] = $v['gold_coin_num'];
                    $spec['num'] = $v['num'];
                    Db::name("goods_spec")->insert($spec);
                }
            }

            // 提交事务
            Db::commit();
            return true;
        }catch(Exception $e)
        {
            Db::rollback();
            return false;
        }

    }
    //商家讲坛
    public function forumSave($data)
    {
        Db::startTrans();
        try{
            if(empty($data['goods_id']))
            {
                #判断添加的唯一性, 用商品名称判断
                self::isOnly(['g_name'=>$data['g_name'],'g_type'=>5]);
                //商品表
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
                $goods['member_id'] = $data['member_id'];
                $goods['store_id'] = self::getSaId($data['member_id']);
                $goods['status'] = 1;
                $goods['source_type'] = 1;
                $goods['g_type'] = 5;
                $goods['create_time'] = time();
                $goods_id = Db::name("goods")
                    ->insertGetId($goods);

                //详情表
                $detail['goods_id'] = $goods_id;
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['is_type'] = $data['type'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
//                $detail['proportion'] = empty($data['proportion']) ? 0 : $data['proportion'];
//                $detail['points_award'] = empty($data['points_award']) ? "" : $data['points_award'];
//                $detail['is_gold_coin'] = empty($data['is_gold_coin']) ? 0 : $data['is_gold_coin'];
//                $detail['gold_coin_num'] = empty($data['gold_coin_num']) ? 0 : $data['gold_coin_num'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];
                $detail['free_time'] = empty($data['free_time']) ? 0 : $data['free_time'];
                Db::name("goods_info")->insert($detail);
                #规格
                $spec['goods_id'] = $goods_id;
                $spec['price'] = $data['price'];
                $spec['proportion'] = $data['proportion'];
                $spec['gold_coin_num'] = $data['gold_coin_num'];
                $spec['cost_price'] = $data['cost_price'];
                Db::name("goods_spec")->insert($spec);
            }else{
                #判断添加的唯一性, 用商品名称判断
                $g_name = $data['g_name'];
                $goods_id = $data['goods_id'];
                $where = "g_name = '$g_name' and goods_id != '$goods_id' and g_type = 5";
                self::isOnly($where);
                //商品表
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
                $goods['update_time'] = time();
                Db::name("goods")
                    ->where(['goods_id'=>$goods_id])
                    ->update($goods);

                //详情表
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['is_type'] = $data['type'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
//                $detail['proportion'] = empty($data['proportion']) ? 0 : $data['proportion'];
//                $detail['points_award'] = empty($data['points_award']) ? "" : $data['points_award'];
//                $detail['is_gold_coin'] = empty($data['is_gold_coin']) ? 0 : $data['is_gold_coin'];
//                $detail['gold_coin_num'] = empty($data['gold_coin_num']) ? 0 : $data['gold_coin_num'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];
                $detail['free_time'] = empty($data['free_time']) ? 0 : $data['free_time'];
                Db::name("goods_info")
                    ->where(['goods_id'=>$goods_id])
                    ->update($detail);
                #规格
                $spec['price'] = $data['price'];
                $spec['cost_price'] = $data['cost_price'];
                $spec['proportion'] = $data['proportion'];
                $spec['gold_coin_num'] = $data['gold_coin_num'];
                Db::name("goods_spec")
                    ->where(['goods_id'=>$goods_id])
                    ->update($spec);

            }

            // 提交事务
            Db::commit();
            return true;
        }catch(Exception $e)
        {
            Db::rollback();
            return false;
        }

    }
    //限时特惠
    public function limitSave($data)
    {
        Db::startTrans();
        try{
            if(empty($data['goods_id']))
            {
                #判断添加的唯一性, 用商品名称判断
                self::isOnly(['g_name'=>$data['g_name'],'g_type'=>4]);
                //商品表
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['member_id'] = $data['member_id'];
                $goods['store_id'] = self::getSaId($data['member_id']);
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
                $goods['status'] = 1;
                $goods['source_type'] = 1;
                $goods['g_type'] = 4;
                $goods['create_time'] = time();
                $goods['limit_number'] = empty($data['limit_registration_num']) ? 0 : $data['limit_registration_num'];
                $goods['limit_start_time'] = empty($data['activity_start_time']) ? 0 : $data['activity_start_time'];
                $goods['limit_end_time'] = empty($data['activity_end_time']) ? 0 : $data['activity_end_time'];
                $goods_id = Db::name("goods")
                    ->insertGetId($goods);
                //详情表
                $detail['goods_id'] = $goods_id;
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['free_gift'] = empty($data['free_gift']) ? "" : $data['free_gift'];
                $detail['consumption_pic'] = empty($data['consumption_pic']) ? "" : $data['consumption_pic'];
                $detail['direct_push'] = empty($data['direct_push']) ? 0 : $data['direct_push'];
                $detail['inter_push'] = empty($data['inter_push']) ? 0 : $data['inter_push'];

                $detail['distribution_method'] = empty($data['distribution_method']) ? "" : $data['distribution_method'];
                $detail['delivery_type'] = empty($data['delivery_type']) ? "" : $data['delivery_type'];
                $detail['delivery_fee'] = empty($data['delivery_fee']) ? 0 : $data['delivery_fee'];
                $detail['is_change'] = empty($data['is_change']) ? 0 : $data['is_change'];
                $detail['is_real'] = empty($data['is_real']) ? 0 : $data['is_real'];
                $detail['is_diff'] = empty($data['is_diff']) ? 0 : $data['is_diff'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];
                Db::name("goods_info")->insert($detail);
                //规格
                $spec['cost_price'] = $data['cost_price'];
                $spec['goods_id'] = $goods_id;;
                $spec['price'] = $data['registration_fee'];
                Db::name("goods_spec")->insert($spec);
            }else{
                #判断添加的唯一性, 用商品名称判断
                $g_name = $data['g_name'];
                $goods_id = $data['goods_id'];
                $where = "g_name = '$g_name' and goods_id != '$goods_id' and g_type = 4";
                self::isOnly($where);
                //商品表
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['member_id'] = $data['member_id'];
                $goods['store_id'] = self::getSaId($data['member_id']);
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
//                $goods['status'] = 1;
                $goods['source_type'] = 1;
                $goods['g_type'] = 4;
                $goods['update_time'] = time();
                $goods['limit_number'] = empty($data['limit_registration_num']) ? 0 : $data['limit_registration_num'];
                $goods['limit_start_time'] = empty($data['activity_start_time']) ? 0 : $data['activity_start_time'];
                $goods['limit_end_time'] = empty($data['activity_end_time']) ? 0 : $data['activity_end_time'];
                Db::name("goods")
                    ->where(['goods_id'=>$goods_id])
                    ->update($goods);
                //详情表
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['free_gift'] = empty($data['free_gift']) ? "" : $data['free_gift'];
                $detail['consumption_pic'] = empty($data['consumption_pic']) ? "" : $data['consumption_pic'];
                $detail['direct_push'] = empty($data['direct_push']) ? 0 : $data['direct_push'];
                $detail['inter_push'] = empty($data['inter_push']) ? 0 : $data['inter_push'];
                $detail['distribution_method'] = empty($data['distribution_method']) ? "" : $data['distribution_method'];
                $detail['delivery_type'] = empty($data['delivery_type']) ? "" : $data['delivery_type'];
                $detail['delivery_fee'] = empty($data['delivery_fee']) ? 0 : $data['delivery_fee'];
                $detail['is_change'] = empty($data['is_change']) ? 0 : $data['is_change'];
                $detail['is_real'] = empty($data['is_real']) ? 0 : $data['is_real'];
                $detail['is_diff'] = empty($data['is_diff']) ? 0 : $data['is_diff'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];
                Db::name("goods_info")
                    ->where(['goods_id'=>$goods_id])
                    ->update($detail);
                //规格
                $spec['cost_price'] = $data['cost_price'];
                $spec['price'] = $data['registration_fee'];
                Db::name("goods_spec")
                    ->where(['goods_id'=>$goods_id])
                    ->update($spec);
            }

            // 提交事务
            Db::commit();
            return true;
        }catch(Exception $e)
        {
            Db::rollback();
            return false;
        }
    }
    //zlc-裂变商品的发布和编辑
    public function fissionSave($data)
    {
        Db::startTrans();
        try{
            if(empty($data['goods_id']))
            {
                #判断添加的唯一性, 用商品名称判断
                self::isOnly(['g_name'=>$data['g_name'],'g_type'=>2]);
                //商品表
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['member_id'] = $data['member_id'];
                $goods['store_id'] = self::getSaId($data['member_id']);
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
                $goods['limit_num_status'] = empty($data['limit_num_status']) ? 0 : $data['limit_num_status'];
                $goods['limit_number'] = empty($data['limit_number']) ? 0 : $data['limit_number'];
                $goods['limit_time_status'] = empty($data['limit_time_status']) ? 0 : $data['limit_time_status'];
                $goods['limit_start_time'] = empty($data['limit_start_time']) ? 0 : $data['limit_start_time'];
                $goods['limit_end_time'] = empty($data['limit_end_time']) ? 0 : $data['limit_end_time'];
                $goods['status'] = 1;
                $goods['source_type'] = 1;
                $goods['g_type'] = 2;
                $goods['create_time'] = time();
                $goods_id = Db::name("goods")
                    ->insertGetId($goods);
                //详情表
                $detail['goods_id'] = $goods_id;
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['distribution_method'] = empty($data['distribution_method']) ? "" : $data['distribution_method'];
                $detail['delivery_type'] = empty($data['delivery_type']) ? "" : $data['delivery_type'];
                $detail['delivery_fee'] = empty($data['delivery_fee']) ? 0 : $data['delivery_fee'];
                $detail['copper_money'] = empty($data['copper_money']) ? 0 : $data['copper_money'];
                $detail['copper_pop'] = empty($data['copper_pop']) ? 0 : $data['copper_pop'];
                $detail['silver_money'] = empty($data['silver_money']) ? 0 : $data['silver_money'];
                $detail['silver_pop'] = empty($data['silver_pop']) ? 0 : $data['silver_pop'];
                $detail['gold_money'] = empty($data['gold_money']) ? 0 : $data['gold_money'];
                $detail['gold_pop'] = empty($data['gold_pop']) ? 0 : $data['gold_pop'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];

                Db::name("goods_info")->insert($detail);

                //规格表
                $specifications = json_decode($data['specifications'],true);
                foreach ($specifications as $k=>$v)
                {
                    $spec['goods_id'] = $goods_id;
                    $spec['name'] = $v['name'];
                    $spec['price'] = $v['price'];
                    $spec['cost_price'] = $v['cost_price'];
                    $spec['num'] = $v['num'];
                    Db::name("goods_spec")->insert($spec);
                }


            }else{
                #判断添加的唯一性, 用商品名称判断
                $g_name = $data['g_name'];
                $goods_id = $data['goods_id'];
                $where = "g_name = '$g_name' and goods_id != '$goods_id' and g_type = 2";
                self::isOnly($where);
                //商品表
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['member_id'] = $data['member_id'];
                $goods['store_id'] = self::getSaId($data['member_id']);
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
                $goods['limit_num_status'] = empty($data['limit_num_status']) ? 0 : $data['limit_num_status'];
                $goods['limit_number'] = empty($data['limit_number']) ? 0 : $data['limit_number'];
                $goods['limit_time_status'] = empty($data['limit_time_status']) ? 0 : $data['limit_time_status'];
                $goods['limit_start_time'] = empty($data['limit_start_time']) ? 0 : $data['limit_start_time'];
                $goods['limit_end_time'] = empty($data['limit_end_time']) ? 0 : $data['limit_end_time'];
//                $goods['status'] = 1;
                $goods['source_type'] = 1;
                $goods['g_type'] = 2;
                $goods['create_time'] = time();
                Db::name("goods")
                    ->where(['goods_id'=>$goods_id])
                    ->update($goods);
                //详情表
                $detail['goods_id'] = $goods_id;
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['distribution_method'] = empty($data['distribution_method']) ? "" : $data['distribution_method'];
                $detail['delivery_type'] = empty($data['delivery_type']) ? "" : $data['delivery_type'];
                $detail['delivery_fee'] = empty($data['delivery_fee']) ? 0 : $data['delivery_fee'];
                $detail['copper_money'] = empty($data['copper_money']) ? 0 : $data['copper_money'];
                $detail['copper_pop'] = empty($data['copper_pop']) ? 0 : $data['copper_pop'];
                $detail['silver_money'] = empty($data['silver_money']) ? 0 : $data['silver_money'];
                $detail['silver_pop'] = empty($data['silver_pop']) ? 0 : $data['silver_pop'];
                $detail['gold_money'] = empty($data['gold_money']) ? 0 : $data['gold_money'];
                $detail['gold_pop'] = empty($data['gold_pop']) ? 0 : $data['gold_pop'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];

                Db::name("goods_info")
                    ->where(['goods_id'=>$goods_id])
                    ->update($detail);

                //规格表
                $specifications = json_decode($data['specifications'],true);
                Db::name("goods_spec")->where(['goods_id'=>$goods_id])->delete();
                foreach ($specifications as $k=>$v)
                {
                    $spec['goods_id'] = $goods_id;
                    $spec['name'] = $v['name'];
                    $spec['price'] = $v['price'];
                    $spec['cost_price'] = $v['cost_price'];
                    $spec['num'] = $v['num'];
                    Db::name("goods_spec")->insert($spec);
                }

            }


            // 提交事务
            Db::commit();
            return true;
        }catch (Exception $e)
        {
            Db::rollback();
            return false;
        }

    }
    //消费爆品发布
    public function explosiveProducts($data)
    {
        Db::startTrans();
        try{
            if(empty($data['goods_id']))
            {
                #判断添加的唯一性, 用商品名称判断
                self::isOnly(['g_name'=>$data['g_name'],'g_type'=>1]);
                //商品表
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['member_id'] = $data['member_id'];
                $goods['store_id'] = self::getSaId($data['member_id']);
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
                $goods['limit_num_status'] = empty($data['limit_num_status']) ? 0 : $data['limit_num_status'];
                $goods['limit_number'] = empty($data['limit_number']) ? 0 : $data['limit_number'];
                $goods['limit_time_status'] = empty($data['limit_time_status']) ? 0 : $data['limit_time_status'];
                $goods['limit_start_time'] = empty($data['limit_start_time']) ? 0 : $data['limit_start_time'];
                $goods['limit_end_time'] = empty($data['limit_end_time']) ? 0 : $data['limit_end_time'];
                $goods['status'] = 1;
                $goods['source_type'] = 1;
                $goods['g_type'] = 1;
                $goods['create_time'] = time();
                $goods_id = Db::name("goods")
                    ->insertGetId($goods);

                //详情表
                $detail['goods_id'] = $goods_id;
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['distribution_method'] = empty($data['distribution_method']) ? "" : $data['distribution_method'];
                $detail['delivery_type'] = empty($data['delivery_type']) ? "" : $data['delivery_type'];
                $detail['delivery_fee'] = empty($data['delivery_fee']) ? 0 : $data['delivery_fee'];
                $detail['is_change'] = empty($data['is_change']) ? 0 : $data['is_change'];
                $detail['is_real'] = empty($data['is_real']) ? 0 : $data['is_real'];
                $detail['is_diff'] = empty($data['is_diff']) ? 0 : $data['is_diff'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];

                Db::name("goods_info")->insert($detail);

                //规格表
                $specifications = json_decode($data['specifications'],true);
                foreach ($specifications as $k=>$v)
                {
                    $spec['goods_id'] = $goods_id;
                    $spec['name'] = $v['name'];
                    $spec['price'] = $v['price'];
                    $spec['cost_price'] = $v['cost_price'];
                    $spec['num'] = $v['num'];
                    $spec['coin_price'] = $v['coin_price'];
                    Db::name("goods_spec")->insert($spec);
                }
            }else{
                #判断修改的唯一性, 用商品名称判断
                $g_name = $data['g_name'];
                $goods_id = $data['goods_id'];
                $where = "g_name = '$g_name' and goods_id != '$goods_id' and g_type = 1";
                self::isOnly($where);
                //商品表
                $goods['g_name'] = empty($data['g_name']) ? "" : $data['g_name'];
                $goods['member_id'] = $data['member_id'];
                $goods['store_id'] = self::getSaId($data['member_id']);
                $goods['cover_pic'] = empty($data['cover_pic']) ? "" : $data['cover_pic'];
                $goods['limit_num_status'] = empty($data['limit_num_status']) ? 0 : $data['limit_num_status'];
                $goods['limit_number'] = empty($data['limit_number']) ? 0 : $data['limit_number'];
                $goods['limit_time_status'] = empty($data['limit_time_status']) ? 0 : $data['limit_time_status'];
                $goods['limit_start_time'] = empty($data['limit_start_time']) ? 0 : $data['limit_start_time'];
                $goods['limit_end_time'] = empty($data['limit_end_time']) ? 0 : $data['limit_end_time'];
//                $goods['status'] = 2;
                $goods['source_type'] = 1;
                $goods['g_type'] = 1;
                $goods['update_time'] = time();
                Db::name("goods")
                    ->where(['goods_id'=>$goods_id])
                    ->update($goods);
                //详情表
                $detail['goods_id'] = $goods_id;
                $detail['video_path'] = empty($data['video_path']) ? "" : $data['video_path'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['img_list'] = empty($data['img_list']) ? "" : $data['img_list'];
                $detail['distribution_method'] = empty($data['distribution_method']) ? "" : $data['distribution_method'];
                $detail['delivery_type'] = empty($data['delivery_type']) ? "" : $data['delivery_type'];
                $detail['delivery_fee'] = empty($data['delivery_fee']) ? 0 : $data['delivery_fee'];
                $detail['is_change'] = empty($data['is_change']) ? 0 : $data['is_change'];
                $detail['is_real'] = empty($data['is_real']) ? 0 : $data['is_real'];
                $detail['is_diff'] = empty($data['is_diff']) ? 0 : $data['is_diff'];
                $detail['product_details_image'] = empty($data['product_details_image']) ? "" : $data['product_details_image'];
                $detail['advertisement'] = empty($data['advertisement']) ? "" : $data['advertisement'];
                Db::name("goods_info")
                    ->where(['goods_id'=>$goods_id])
                    ->update($detail);
                //规格表
                $specifications = json_decode($data['specifications'],true);

                Db::name("goods_spec")->where(['goods_id'=>$goods_id])->delete();
                foreach ($specifications as $k=>$v)
                {
                    $spec['goods_id'] = $goods_id;
                    $spec['name'] = $v['name'];
                    $spec['price'] = $v['price'];
                    $spec['cost_price'] = $v['cost_price'];
                    $spec['num'] = $v['num'];
                    $spec['coin_price'] = $v['coin_price'];
                    Db::name("goods_spec")->insert($spec);
                }



            }
            // 提交事务
            Db::commit();
            return true;

        }catch (Exception $e)
        {
            Db::rollback();
            return false;
        }
    }
    //判断商品发布是否唯一, 根据商品名称
    public function isOnly($where)
    {
        $count = Db::name("goods")
            ->where($where)
            ->count();
        if($count)
        {
            returnJson(-2004,"该商品名称已存在");
        }
    }
    //根据用户id获取商品id
    private function getSaId($member_id)
    {
        $data = Db::name("store_apply")
            ->field("sa_id")
            ->where(['member_id'=>$member_id])
            ->find();
        return $data['sa_id'];
    }

    /**
     * 精选
     *
     */
    public function carefullyChosen($current_page,$page_num,$input){
        $type=$input['type'];
        $longitude=$input['longitude'];
        $latitude=$input['latitude'];
        if($type==1){//优选商家
            $where=[];
            $where['g_type']=['in',[1,2]];
            $where['source_type']=1;
            $where['status']=1;
            $where['is_delete']=1;

            $tm=time();
            $where1=['limit_time_status' =>2,'limit_start_time'=>['egt',$tm],'limit_end_time'=>['elt',$tm]];
            $where2=['limit_time_status' =>1];
            $data = Db::name("goods")
                ->alias("g")
                ->field("store_id,goods_id,g_name,cover_pic,sale_total")
                ->where($where)
                ->where(function ($query) use ( $where1,$where2) {
                    $query->where(function($q) use ( $where1){
                        $q->where( $where1);
                    })->whereOR($where2);
                })
                ->page($current_page,$page_num)
                ->select();

            $storeIDs=array_column($data,'store_id');
            //获取店铺位置
            $storeList = Db::name("store_apply")
                ->field("sa_id,longitude,latitude")
                ->where(['sa_id'=>['in', $storeIDs]])
                ->column("longitude,latitude","sa_id");


            $goodsIDS=array_column($data,'goods_id');
            $goodsSpec=Db::name("goods_spec")->where(['goods_id'=>['in',$goodsIDS]])->column("price,cost_price,coin_price",'goods_id');
            foreach ( $data as &$item){
                $item['price']=$goodsSpec[$item['goods_id']]['price'];
                $item['cost_price']=$goodsSpec[$item['goods_id']]['cost_price'];
                $item['coin_price']=$goodsSpec[$item['goods_id']]['coin_price'];
                $item['distance']=round(get_distance($longitude,$latitude,$storeList[$item['store_id']]["longitude"],$storeList[$item['store_id']]["latitude"]));
            }
            $total_num = Db::name("goods")
                ->alias("g")
                ->where($where)
                ->count();
            $list['current_page'] = $current_page;
            $list['total_page'] = ceil($total_num / $page_num);
            $list['page_num'] = $page_num;
            $list['total_num'] = $total_num;
            $list['data'] = $data;
            return $list;
        }elseif($type==2){//活动精选
            $where=[];
            $where['g_type']=4;
            $where['source_type']=1;
            $where['status']=1;
            $where['is_delete']=1;

            $tm=time();
            $where1=['limit_time_status' =>2,'limit_start_time'=>['egt',$tm],'limit_end_time'=>['elt',$tm]];
            $where2=['limit_time_status' =>1];
            $data = Db::name("goods")
                ->alias("g")
                ->field("goods_id,g_name,cover_pic,limit_start_time,limit_end_time,store_id")
                ->where($where)
                ->where(function ($query) use ( $where1,$where2) {
                    $query->where(function($q) use ( $where1){
                        $q->where( $where1);
                    })->whereOR($where2);
                })
                ->page($current_page,$page_num)
                ->select();
            $goodsIDS=array_column($data,'goods_id');
            $storeIDS=array_column($data,'store_id');
            $goodsSpec=Db::name("goods_spec")->where(['goods_id'=>['in',$goodsIDS]])->column("price,cost_price,coin_price",'goods_id');
            $goodsStore=Db::name("store_apply")->where(['sa_id'=>['in',$storeIDS]])->column("company_name",'sa_id');

            //获取店铺位置
            $storeList = Db::name("store_apply")
                ->field("sa_id,longitude,latitude")
                ->where(['sa_id'=>['in',  $storeIDS]])
                ->column("longitude,latitude","sa_id");
            foreach ( $data as &$item){
                $item['price']=$goodsSpec[$item['goods_id']]['price'];
                $item['cost_price']=$goodsSpec[$item['goods_id']]['cost_price'];
                $item['coin_price']=$goodsSpec[$item['goods_id']]['coin_price'];
                $item['company_name']=$goodsStore[$item['store_id']];
                $item['distance']=round(get_distance($longitude,$latitude,$storeList[$item['store_id']]["longitude"],$storeList[$item['store_id']]["latitude"]));
            }
            $total_num = Db::name("goods")
                ->alias("g")
                ->where($where)
                ->count();
            $list['current_page'] = $current_page;
            $list['total_page'] = ceil($total_num / $page_num);
            $list['page_num'] = $page_num;
            $list['total_num'] = $total_num;
            $list['data'] = $data;
            return $list;
        }
    }

}